package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class loginFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        /*
        转换为httpservletrequest和httpServletResponse
         */
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;

        String servletPath = request.getServletPath();

        System.out.println("用户请求的URI："+servletPath);

        if (
                !servletPath.equals("/login.jsp") && !servletPath.equals("/loginServlet")
                && !servletPath.endsWith(".js") && !servletPath.endsWith(".css")
                && !servletPath.equals("/resources/img/login_tit.png")
                && !servletPath.equals("/resources/img/login_bt.png")
                && !servletPath.equals("/resources/img/login_bgi.gif")
        ) {
            HttpSession session = request.getSession(false);
            if(session == null || session.getAttribute("us") == null){

                request.getRequestDispatcher("login.jsp").forward(request,response);
                return;
            }
        }


        chain.doFilter(request, response);
    }


}
